﻿using System;
using System.Collections.Generic;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit;

namespace HYOpeningCommon.Public
{
	public class DBOper3DView
	{
		public static View3D GetHY3DView(UIDocument uiDoc)
		{
			Document document = uiDoc.Document;
			if (document.ActiveView is View3D && document.ActiveView.Name == "盈建科三维视图")
			{
				return document.ActiveView as View3D;
			}
			IList<Element> list = new FilteredElementCollector(document).OfCategory(BuiltInCategory.OST_Views).WhereElementIsNotElementType().ToElements();
			foreach (Element element in list)
			{
				View3D view3D = element as View3D;
				if (view3D != null && view3D.Name == "盈建科三维视图")
				{
					uiDoc.ActiveView = view3D;
					return view3D;
				}
			}
			Transaction transaction = new Transaction(document, "DuplicateView3D");
			View3D result;
			try
			{
				foreach (Element element2 in list)
				{
					View3D view3D2 = element2 as View3D;
					if (view3D2 != null)
					{
						string viewName = view3D2.ViewName;
						if (view3D2.CanViewBeDuplicated(0))
						{
							transaction.Start();
							ElementId elementId = view3D2.Duplicate(0);
							if (elementId.IntegerValue != -1)
							{
								View3D view3D3 = document.GetElement(elementId) as View3D;
								view3D3.Name = "盈建科三维视图";
								Parameter parameter = view3D3.GetParameter(BuiltInParameter.VIEW_TEMPLATE);
								if (parameter != null)
								{
									parameter.Set(ElementId.InvalidElementId);
								}
								transaction.Commit();
								uiDoc.ActiveView = view3D3;
								return view3D3;
							}
							transaction.RollBack();
						}
					}
				}
				result = null;
			}
			catch (Exception)
			{
				transaction.RollBack();
				result = null;
			}
			return result;
		}

		public static void SetViewStyle(ref View3D view3D, UIDocument uiDoc)
		{
			if (view3D == null)
			{
				return;
			}
			Transaction transaction = new Transaction(uiDoc.Document, "SetViewStyle");
			try
			{
				transaction.Start();
				if ((int)view3D.DetailLevel != 3 && view3D.CanModifyDetailLevel())
				{
					view3D.DetailLevel = (ViewDetailLevel)3;
				}
				if ((int)view3D.DisplayStyle != 4 && view3D.CanModifyDisplayStyle())
				{
                    view3D.DisplayStyle = (DisplayStyle)4;
				}
				XYZ xyz = new XYZ(1.0, 1.0, -1.0);
				XYZ xyz2 = xyz;
				XYZ xyz3 = xyz;
				XYZ xyz4 = new XYZ(xyz.X, xyz.Y, 0.0);
				XYZ xyz5;
				if (xyz4.IsAlmostEqualTo(XYZ.Zero))
				{
					xyz5 = XYZ.BasisY;
				}
				else
				{
					xyz4 = DBOper3DView.RotateTo(xyz4, -Math.PI*.5, XYZ.BasisZ);
					xyz5 = DBOper3DView.RotateTo(xyz3, Math.PI*.5, xyz4);
				}
				ViewOrientation3D orientation = new ViewOrientation3D(xyz2, xyz5, xyz3);
				view3D.SetOrientation(orientation);
				Parameter parameter = view3D.get_Parameter(BuiltInParameter.VIEWER_CROP_REGION);
				if (parameter != null && parameter.AsInteger() == 1)
				{
					parameter.Set(0);
				}
				transaction.Commit();
			}
			catch (Exception)
			{
				transaction.RollBack();
				throw;
			}
		}

		private static XYZ RotateTo(XYZ List, double angle, XYZ axis)
		{
			return RevitVersionFuncs.CreateRotationAtPoint(axis, angle, new XYZ(0.0, 0.0, 0.0)).OfPoint(List);
		}

		public static void SetViewCutVisible(ref View3D view, Autodesk.Revit.DB.Document doc)
		{
			if (view == null)
			{
				return;
			}
			try
			{
				BuiltInCategory builtInCategory = BuiltInCategory.OST_DuctCurves;
				Category category = doc.Settings.Categories.get_Item(builtInCategory);
				DBOper3DView.SetCategoryVisible(ref view, category);
				builtInCategory = BuiltInCategory.OST_CableTray;
				category = doc.Settings.Categories.get_Item(builtInCategory);
				DBOper3DView.SetCategoryVisible(ref view, category);
				builtInCategory = BuiltInCategory.OST_PipeCurves;
				category = doc.Settings.Categories.get_Item(builtInCategory);
				DBOper3DView.SetCategoryVisible(ref view, category);
				builtInCategory = BuiltInCategory.OST_ElectricalEquipment;
				category = doc.Settings.Categories.get_Item(builtInCategory);
				DBOper3DView.SetCategoryVisible(ref view, category);
				builtInCategory = BuiltInCategory.OST_MechanicalEquipment;
				category = doc.Settings.Categories.get_Item(builtInCategory);
				DBOper3DView.SetCategoryVisible(ref view, category);
			}
			catch (Exception)
			{
			}
		}

		private static void SetCategoryVisible(ref View3D view, Category category)
		{
			Transaction transaction = new Transaction(view.Document, "SetCategoryVisible");
			try
			{
				transaction.Start();
				if (!view.GetYJKVisibility(category))
				{
					view.SetYJKVisibility(category, true);
				}
				transaction.Commit();
			}
			catch (Exception)
			{
				transaction.RollBack();
				throw;
			}
		}
	}
}
